package br.com.sigeden.dao;

import br.com.sigeden.model.Cabelo;
import br.com.sigeden.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author David Amom <davidamom@gmail.com>
 */
public class CabeloDAO {

    private Session sessao;
    private Transaction transacao;
    private List<Cabelo> cabelos;

    public List<Cabelo> listarCabelos() {
        sessao = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = sessao.createCriteria(Cabelo.class);
        this.cabelos = criteria.list();

        sessao.close();
        return cabelos;
    }

    public void adicionarCabelo(Cabelo cabelo) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            Cabelo c = new Cabelo();

            c.setCor(cabelo.getCor());
            c.setTipo(cabelo.getTipo());
            c.setEnvolvidos(c.getEnvolvidos());

            sessao.save(c);
            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void removerCabelo(Cabelo cabelo) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.delete(cabelo);

            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void atualizarCabelo(Cabelo cabelo) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.update(cabelo);

            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }
}
